package com.itrip.auth.service;

import com.itrip.auth.exception.LoginNotFountException;
import com.itrip.beans.domain.ItripUser;
import com.itrip.beans.vo.TokenVo;
import com.itrip.dao.ItripUserMapper;
import com.itrip.utils.MD5;
import com.itrip.utils.ToKenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
    @Autowired
    private ToKenUtil toKenUtil;
    @Autowired
    private ItripUserMapper itripUserMapper;

    public ItripUser login(String name, String password) throws LoginNotFountException {
        ItripUser user = itripUserMapper.selectByUserCode(name);
        if (user == null) {
            throw new LoginNotFountException("用户名不存在");
        }
        if(!user.getUserPassword().equals(MD5.md5(password))){
            throw new LoginNotFountException("密码输入错误!");
        }
        if(user.getActivated() == 0){
            throw new LoginNotFountException("当前账户未激活");
        }
        return user;
    }

    public TokenVo createToken(ItripUser user,String userAgent){
        return toKenUtil.createToken(user,userAgent);
    }
}
